package cn.xfdzx.android.apps.shop.activity.myshop;


import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnCheckedChanged;
import butterknife.OnClick;
import cn.xfdzx.android.apps.shop.R;
import cn.xfdzx.android.apps.shop.activity.login.OneLoginMainActivity;
import cn.xfdzx.android.apps.shop.apps.Constants;
import cn.xfdzx.android.apps.shop.base.BaseFragmentMvpActivity;
import cn.xfdzx.android.apps.shop.bean.ShopInDetailsBeen;
import cn.xfdzx.android.apps.shop.bean.StoreAmountBean;
import cn.xfdzx.android.apps.shop.contract.MyShopCenterContract;
import cn.xfdzx.android.apps.shop.fragment.ShopManagerFragment;
import cn.xfdzx.android.apps.shop.fragment.ShopmMyFragment;
import cn.xfdzx.android.apps.shop.presenter.MyShopCenterPresenter;
import cn.xfdzx.android.apps.shop.util.ProgressDialog;
import cn.xfdzx.android.apps.shop.util.StatusBarUtil;
import cn.xfdzx.android.apps.shop.util.UtilsData;

import static cn.xfdzx.android.apps.shop.apps.Constants.APP_TOKEN;

public class MyShopCenterActivity extends BaseFragmentMvpActivity<MyShopCenterPresenter> implements MyShopCenterContract.View {


    @BindView(R.id.iv_back)
    ImageView ivBack;
     @BindView(R.id.ll_state)
    LinearLayout llState;
    @BindView(R.id.rl_title)
    RelativeLayout rlTitle;
    @BindView(R.id.rb_shop_1)
    RadioButton rbShop_1;
    @BindView(R.id.rb_shop_2)
    RadioButton rbShop_2;
    private Context mContext;
    private List<Fragment> mFragments;
    private FragmentManager mFragmentManager;
    private int mCurrentItem = -1;
    private ShopManagerFragment shopManagerFragment;
    private ShopmMyFragment shopmMyFragment;
    private int type = 0;
    private int position = 1;
    private RelativeLayout.LayoutParams item_bar;
    public ShopInDetailsBeen beans;

    @Override
    public int getLayoutId() {
        return R.layout.activity_my_shop_center;
    }

    @Override
    public void initView() {
        initHigh();
        mContext = MyShopCenterActivity.this;
        mPresenter = new MyShopCenterPresenter();
        mPresenter.attachView(this);
        StatusBarUtil.setLightStatusBar(this, false, true);
        shopManagerFragment = ShopManagerFragment.newInstance(1);
        shopmMyFragment = ShopmMyFragment.newInstance(1);
        initParams();
        //设置默认选中
        rbShop_1.setChecked(true);
        ivBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        mPresenter.shop_in_details();
    }


    private void initHigh() {
        item_bar = (RelativeLayout.LayoutParams) llState.getLayoutParams();
        item_bar.height = StatusBarUtil.getStatusBarHeight(this);
        llState.setLayoutParams(item_bar);
    }
    protected void initParams() {
        //注意fragments数量和radioBtn数量要对应
        mFragments = new ArrayList<>();
        mFragments.add(shopManagerFragment);//
        mFragments.add(shopmMyFragment);//

        mFragmentManager = getSupportFragmentManager();
    }

    @Override
    public void showLoading() {
        if (!ProgressDialog.getInstance().materialDialog.isShowing()) {
            ProgressDialog.getInstance().show(this);
        }
    }

    @Override
    public void hideLoading() {

            ProgressDialog.getInstance().dismiss();

    }

    @Override
    public void onError(Throwable throwable) {

    }

    @Override
    public void onSuccess(ShopInDetailsBeen bean) {
        if (bean.getStatus() == Constants.SUCCESS_CODE) {
            shopManagerFragment.setData(bean);
            beans=bean;
            type = bean.getData().getSales_model();
            //保存店铺id
            UtilsData.getPreference(this, "login").put("storeId", bean.getData().getId()+"");
            //保存店铺类型
            UtilsData.getPreference(this, "login").put("storetype", bean.getData().getSales_model()+"");
            if (type==0){
                llState.setBackgroundColor(getResources().getColor(R.color.color_FB9129));
                rlTitle.setBackgroundColor(getResources().getColor(R.color.color_FB9129));
            }else {
                llState.setBackgroundColor(getResources().getColor(R.color.color_02C557));
                rlTitle.setBackgroundColor(getResources().getColor(R.color.color_02C557));
            }
            setBottom(type);
            setColor(type);
        } else {
            if (bean.getStatus() == Constants.LOGIN_CODE) {
                UtilsData.getPreference(this, "login").remove("head_url");
                UtilsData.getPreference(this, "login").remove("nickname");
                UtilsData.getPreference(this, "login").remove("is_store");
                UtilsData.getPreference(this, "login").remove("token");
                APP_TOKEN = "";
                Intent intent = new Intent(this, OneLoginMainActivity.class);
                startActivity(intent);
                return;
            }
            Toast.makeText(this, bean.getMsg(), Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void getStoreAmountSuccess(StoreAmountBean bean) {

    }


    @OnCheckedChanged({R.id.rb_shop_1, R.id.rb_shop_2})
    public void checkRadioBtn(CompoundButton buttonView, boolean isChecked) {
        if (isChecked) {
            int viewId = buttonView.getId();
            if (viewId == R.id.rb_shop_1) {
                position = 1;
                changeFragment(0);

            } else if (viewId == R.id.rb_shop_2) {
                position = 2;
                changeFragment(1);

            }
            setColor(type);
        }
    }

    public void setColor(int type) {
        if (type == 0) {
            if (position == 1) {
                rbShop_1.setTextColor(getResources().getColor(R.color.color_FB9129));
            } else {
                rbShop_1.setTextColor(getResources().getColor(R.color.color_565656));
            }
            if (position == 2) {
                rbShop_2.setTextColor(getResources().getColor(R.color.color_FB9129));
            } else {
                rbShop_2.setTextColor(getResources().getColor(R.color.color_565656));
            }
        } else {
            if (position == 1) {
                rbShop_1.setTextColor(getResources().getColor(R.color.color_02C557));
            } else {
                rbShop_1.setTextColor(getResources().getColor(R.color.color_565656));
            }
            if (position == 2) {
                rbShop_2.setTextColor(getResources().getColor(R.color.color_02C557));
            } else {
                rbShop_2.setTextColor(getResources().getColor(R.color.color_565656));
            }

        }

    }

    /**
     * 切换fragment
     *
     * @param i
     */
    private void changeFragment(int i) {
        Log.i("TAG", "i:" + i + " mCurrentItem:" + mCurrentItem);
        if (mCurrentItem == i) return; //若选中的item为当前，返回
        //这里不推荐使用replace()
        FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();
        if (mFragments.get(i).isAdded()) {
            fragmentTransaction.hide(mFragments.get(mCurrentItem)).show(mFragments.get(i));
        } else {
            if (mCurrentItem == -1) {//初始状态新建
                fragmentTransaction.add(R.id.fragment_my_shop, mFragments.get(i));
            } else {//非初始状态切换
                fragmentTransaction.hide(mFragments.get(mCurrentItem)).add(R.id.fragment_my_shop, mFragments.get(i));
            }
        }
        fragmentTransaction.commit();
        mCurrentItem = i;
    }

    public void setBottom(int isRetail) {
        if (isRetail == 0) {
            Drawable nav_up1 = getResources().getDrawable(R.drawable.main_rb_shop_select_orange);
            nav_up1.setBounds(0, 0, nav_up1.getMinimumWidth(), nav_up1.getMinimumHeight());
            rbShop_1.setCompoundDrawables(null, nav_up1, null, null);
            Drawable nav_up2 = getResources().getDrawable(R.drawable.main_rb_my_orange);
            nav_up2.setBounds(0, 0, nav_up2.getMinimumWidth(), nav_up2.getMinimumHeight());
            rbShop_2.setCompoundDrawables(null, nav_up2, null, null);
            setColor(0);
        } else {
            Drawable nav_up1 = getResources().getDrawable(R.drawable.main_rb_shop_select_green);
            nav_up1.setBounds(0, 0, nav_up1.getMinimumWidth(), nav_up1.getMinimumHeight());
            rbShop_1.setCompoundDrawables(null, nav_up1, null, null);
            Drawable nav_up2 = getResources().getDrawable(R.drawable.main_rb_my_select);
            nav_up2.setBounds(0, 0, nav_up2.getMinimumWidth(), nav_up2.getMinimumHeight());
            rbShop_2.setCompoundDrawables(null, nav_up2, null, null);

            setColor(1);
        }

    }

    @Override
    protected boolean isUseFullScreenMode() {
        return true;
    }
}
